package com.hclc.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author lixin
 * @ClassName: WebMvcConfig
 * @Description: TODO(web配置)
 * @date 2019年5月2日 下午7:17:23
 */
@Configuration
@Slf4j
public class WebMvcConfig implements WebMvcConfigurer {

    private String port;

    private static List<String> inCludeRoutes = new ArrayList<>();

    private static List<String> exCludeRoutes = new ArrayList<>();

    static {
        inCludeRoutes.add("/**");
        //放开静态资源拦截
        exCludeRoutes.addAll(Arrays.asList("/static/**", "/**/api/**", "/favicon.ico", "/console/user/**"));
    }

    @Autowired
    private WebInterceptor webInterceptor;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "DELETE", "PUT")
                .maxAge(3600);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//		registry.addInterceptor(new WebInterceptor()); //这种方式，webInterceptor使用 @Autowired 的类会注入失败
        registry.addInterceptor(webInterceptor)
                .addPathPatterns(inCludeRoutes)
                .excludePathPatterns(exCludeRoutes);
        log.debug(">>>>>>>>>>>>>> 拦截器注册完毕<<<<<<<<<<<<<");
    }

    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverterFactory(new IntegerCodeToEnumConverterFactory());
        registry.addConverterFactory(new StringCodeToEnumConverterFactory());
    }


}
